package backend.product.manager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import core.product.model.Product;
import core.product.model.ProductAv;
import core.util.HibernateCoreUtil;
import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;

public class ProductAttributeSetManager extends HibernateCoreUtil
{
	public List<AttributeSet> getAssignedAttributeSet(Product p)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<AttributeSet> asLst = new ArrayList<AttributeSet>();
		List<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p group by attributeSet.name").setEntity("p", p).list();	
		Iterator<ProductAv> pavItr = pavlst.iterator();
		while(pavItr.hasNext())
		{
			asLst.add(pavItr.next().getAttributeSet());
		}
		session.getTransaction().commit();
		return asLst;
	}
	
	public List<AttributeSet> getUnAssignedAttributeSet(Product p)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Iterator<AttributeSet> attrsets = session.createQuery("from AttributeSet").list().iterator();
		List<AttributeSet> unassignedattrset = new ArrayList<AttributeSet>();
		while(attrsets.hasNext())
		{
			AttributeSet attrset = attrsets.next();
			List<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p and attributeSet=:as").setEntity("p", p).setEntity("as", attrset).list();
			if(pavlst.size()==0)
			{
				unassignedattrset.add(attrset);
			}
		}
		
		session.getTransaction().commit();
		return unassignedattrset ;
	}
	
	public void assignAttributeSet(Product p,AttributeSet as,Attribute a)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		ProductAv pav = null ;
		List<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p and attributeSet=:as and attribute=:a").setEntity("p", p).setEntity("as", as).setEntity("a", a).list();
		if(pavlst.size() > 0)
		{
			pav = pavlst.get(0);
		}
		else
		{
			pav = new ProductAv();
		}
		pav.setProduct(p);
		pav.setAttributeSet(as);
		pav.setAttribute(a);
			session.merge(pav);
		
		session.getTransaction().commit();
	}
	public List<ProductAv> getProductAvByAttributeSet(AttributeSet as)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductAv> pavlst = session.createQuery("from ProductAv where attributeSet=:as").setEntity("as", as).list();
		session.getTransaction().commit();
		return pavlst ;
	}
	public List<ProductAv> getProductAvByAttributeSetAndProduct(Product p,AttributeSet as)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p and attributeSet=:as").setEntity("p", p).setEntity("as", as).list();
		session.getTransaction().commit();
		return pavlst ;
	}
	public ProductAv  getProductAvByAttributeSetAndProductAndAttribute(Product p,AttributeSet as, Attribute a)
	{
		ProductAv pav = null ;
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p and attributeSet=:as and attribute=:a").setEntity("p", p).setEntity("as", as).setEntity("a", a).list();
		session.getTransaction().commit();
		if(pavlst.size() > 0)
		{
			pav = pavlst.get(0);
		}
		return pav ;
	}
	public void unassignAttributeSet(Product p,AttributeSet as)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Iterator<ProductAv> pavlst = session.createQuery("from ProductAv where product=:p and attributeSet=:as").setEntity("p", p).setEntity("as", as).list().iterator();
		while(pavlst.hasNext())
		{
			ProductAv localPav = pavlst.next();
			localPav.setAttribute(null);
			localPav.setAttributeSet(null);
			localPav.setProduct(null);
			localPav.setValue(null);
			localPav.setAttributeValues(null);
			session.delete(localPav);
		}
		session.getTransaction().commit();
	}
	public void save(ProductAv pav)
	{
		Session session = HibernateCoreUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(pav);
		session.getTransaction().commit();
	}
}
